<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_good_category_add_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
                
        $result = array('status' => 0, 'message' => '', 'data' => array());
         
        $parent_id = init_user_data_key($_POST, 'parent_id', 'int');
        $name      = init_user_data_key($_POST, 'name', 'str');
        $order     = init_user_data_key($_POST, 'order', 'int');
        $status    = init_user_data_key($_POST, 'status', 'int');
         
        if(empty($name)){
            $result['status']  = -1;
            $result['message'] = '请输入分类名称';
        }else if((empty($parent_id) && $parent_id !== 0) || $parent_id < 0){
            $result['status']  = -2;
            $result['message'] = '请选择上级分类';
        }else if((empty($order) && $order !== 0) || $order < 0){
            $result['status']  = -3;
            $result['message'] = '请输入排序';
        }else if((empty($status) && $status !== 0) || $status < 0){
            $result['status']  = -4;
            $result['message'] = '请选择状态';
        }else{
            $good_category_info   = array(
                'parent_id' => $parent_id,
                'name' => $name,
                'order' => $order,
                'status' => $status,
            );
             
            $create_time = time();
            $good_category_info['create_time'] = isset($_POST['create_time']) ? intval($_POST['create_time']) : $create_time;
            $good_category_info['update_time'] = isset($_POST['update_time']) ? intval($_POST['update_time']) : $create_time;
             
             
            $write_db = $this->_get_write_db();
            $pdo = $write_db->get_connection();
            $pdo->beginTransaction();
             
            try{
                $model_background_good_category = wwy_resource_container::get_model_background_good_category();
                 
                $add_result = $model_background_good_category->add($good_category_info);
                $good_category_id = $add_result;
                $good_category_info['id'] = $good_category_id;
                 
                if(!$add_result){
                    $result['status']  = -5;
                    $result['message'] = '新增失败';
                }else{
                    $result['status']  = 1;
                    $result['message'] = '新增成功';
                }
                 
                wwy_resource_container::get_cache_handler()->flush();
                 
                $pdo->commit();
            }catch (Exception $e){
                $pdo->rollBack();
                 
                throw $e;
            }
        }
         
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}